package com.sunmouse.toollib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sunmouse.toollib.R;
import com.sunmouse.toollib.utils.MetricsUtils;
import com.sunmouse.toollib.utils.StringUtils;


/**
 * Created by Kvin on 2016/1/5.
 */
public class ItemLinearLayout extends LinearLayout {
    //set Paint
    private final Rect mRect = new Rect();
    private Paint linePaint;

    private int lineType;
    private int mMode;
    private int leftPic;
    private int rightPic;
    private String txt;
    private String subTxt;

    //左右边距及leftText与content文本间距
    private int marginLeft;
    private int marginRight;
    private int marginGap;
    private int subMarginGap;

    private float txtSize;
    private int txtColor;
    private float subTxtSize;
    private int subTxtColor;
    private int lineColor;

    private int winWidth;

    private ImageView imgLeft;
    private ImageView imgRight;
    private TextView content;
    private TextView subContent;

    public ItemLinearLayout(Context context) {
        super(context);
    }

    public ItemLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        setWillNotDraw(false);//call onDraw() Method
        //loading XML attributes
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ItemLinearLayout);
        leftPic = typedArray.getResourceId(R.styleable.ItemLinearLayout_illLeftImg, 0);
        rightPic = typedArray.getResourceId(R.styleable.ItemLinearLayout_illRightImg, 0);
        txt = typedArray.getString(R.styleable.ItemLinearLayout_illTxt);
        txtColor = typedArray.getColor(R.styleable.ItemLinearLayout_illTxtColor, Color.BLACK);
        txtSize = typedArray.getDimension(R.styleable.ItemLinearLayout_illTxtSize, 5);
        subTxt = typedArray.getString(R.styleable.ItemLinearLayout_illSubTxt);
        subTxtColor = typedArray.getColor(R.styleable.ItemLinearLayout_illSubTxtColor, Color.GRAY);
        subTxtSize = typedArray.getDimension(R.styleable.ItemLinearLayout_illSubTxtSize, 5);

        lineColor = typedArray.getColor(R.styleable.ItemLinearLayout_illLineColor, Color.GRAY);
        marginLeft = typedArray.getDimensionPixelSize(R.styleable.ItemLinearLayout_illMarginLeft, 0);
        marginRight = typedArray.getDimensionPixelSize(R.styleable.ItemLinearLayout_illMarginRight, 0);
        marginGap = typedArray.getDimensionPixelSize(R.styleable.ItemLinearLayout_illMarginGap, 0);
        subMarginGap = typedArray.getDimensionPixelSize(R.styleable.ItemLinearLayout_illSubMarginGap, 5);
        lineType = typedArray.getInt(R.styleable.ItemLinearLayout_illLineType, LineType.TYPE_FILL);
        mMode = typedArray.getInt(R.styleable.ItemLinearLayout_illMode, Mode.COMMON);
        typedArray.recycle();
        setView(context);

        DisplayMetrics mMetrics = MetricsUtils.winMetrics(context);
        winWidth = mMetrics.widthPixels;
        //init linePaint
        linePaint = new Paint();//use to draw line
        linePaint.setColor(lineColor);
        linePaint.setStrokeWidth(1.0f);

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        mRect.left = 0;
        mRect.right = 0;
        if (imgLeft != null) {
            mRect.top = (getHeight() - imgLeft.getMeasuredHeight()) / 2;
            mRect.left = marginLeft;
            mRect.right = mRect.left + imgLeft.getMeasuredWidth();
            mRect.bottom = (getHeight() + imgLeft.getMeasuredHeight()) / 2;
            imgLeft.layout(mRect.left, mRect.top, mRect.right, mRect.bottom);
        }
        if (content != null) {
            mRect.top = (getHeight() - content.getMeasuredHeight()) / 2;
            mRect.bottom = (getHeight() + content.getMeasuredHeight()) / 2;
            if (mMode == Mode.BUTTON) {
                mRect.left = (getWidth() - content.getMeasuredWidth()) / 2;
                mRect.right = (getWidth() + content.getMeasuredWidth()) / 2;
            } else {
                mRect.left = (imgLeft == null) ? marginLeft : (mRect.right + marginGap);
                mRect.right = mRect.left + content.getMeasuredWidth();
            }
            content.layout(mRect.left, mRect.top, mRect.right, mRect.bottom);
        }
        if (subContent != null) {
            mRect.top = (getHeight() - subContent.getMeasuredHeight()) / 2;
            int rw = imgRight == null ? 0 : imgRight.getMeasuredWidth();
            mRect.left = winWidth - marginRight - rw - subMarginGap - subContent.getMeasuredWidth();
            mRect.right = mRect.left + subContent.getMeasuredWidth();
            mRect.bottom = (getHeight() + subContent.getMeasuredHeight()) / 2;
            subContent.layout(mRect.left, mRect.top, mRect.right, mRect.bottom);
        }
        if (imgRight != null) {
            mRect.top = (getHeight() - imgRight.getMeasuredHeight()) / 2;
            mRect.left = winWidth - marginRight - imgRight.getMeasuredWidth();
            mRect.right = mRect.left + imgRight.getMeasuredWidth();
            mRect.bottom = (getHeight() + imgRight.getMeasuredHeight()) / 2;
            imgRight.layout(mRect.left, mRect.top, mRect.right, mRect.bottom);
        }
    }

    /**
     * consider views added to container
     *
     * @param context
     */
    private void setView(Context context) {
        //add widgets to ViewGroup
        this.setOrientation(HORIZONTAL);
        if (mMode == Mode.BUTTON) {
            addContent(context);
        } else if (mMode == Mode.COMMON) {
            addLeftImage(context);
            addContent(context);
            addRightImage(context);
        } else if (mMode == Mode.UPDATE) {
            addLeftImage(context);
            addContent(context);
            addSubContent(context);
            addRightImage(context);
        }

    }

    /**
     * add left image view  (display a little icon commonly)
     *
     * @param context
     */
    private void addRightImage(Context context) {
        if (rightPic == 0) return;
        imgRight = new ImageView(context);
        imgRight.setImageResource(rightPic);
        this.addView(imgRight);
    }

    /**
     * add sub text view (use for showing desc content)
     *
     * @param context
     */
    private void addSubContent(Context context) {
        subContent = new TextView(context);
        subContent.setTextColor(subTxtColor);
        subContent.setText(subTxt);
        subContent.setTextSize(TypedValue.COMPLEX_UNIT_PX, subTxtSize);//attention to the unit of txtSize
        this.addView(subContent);
    }

    /**
     * add content view
     *
     * @param context
     */
    private void addContent(Context context) {
        content = new TextView(context);
        content.setTextColor(txtColor);
        content.setText(txt);
        content.setTextSize(TypedValue.COMPLEX_UNIT_PX, txtSize);//attention to the unit of txtSize
        this.addView(content);
    }


    /**
     * add left image view
     *
     * @param context
     */
    private void addLeftImage(Context context) {
        if (leftPic == 0) return;
        imgLeft = new ImageView(context);
        imgLeft.setImageResource(leftPic);
        this.addView(imgLeft);
    }

    /**
     * calculate third control`s margin left
     *
     * @param metrics
     * @return
     */
    private int getThirdMarginLeft(DisplayMetrics metrics) {
        if (imgLeft != null)
            return metrics.widthPixels - marginRight - marginGap - marginLeft - MetricsUtils.conMetrics(imgLeft).getMeasuredWidth() - MetricsUtils.conMetrics(content).getMeasuredWidth() - MetricsUtils.conMetrics(imgRight).getMeasuredWidth();
        return metrics.widthPixels - marginRight - marginGap - marginLeft - MetricsUtils.conMetrics(content).getMeasuredWidth() - MetricsUtils.conMetrics(imgRight).getMeasuredWidth();

    }

    public ItemLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawLine(canvas, lineType);
    }

    //draw line
    private void drawLine(Canvas canvas, int lineType) {
        if (lineType == LineType.TYPE_NOT) return;

        int startX = -1;
        if (lineType == LineType.TYPE_FILL) {
            startX = 1;
        } else if (lineType == LineType.TYPE_LEAVE_LEFT) {
            startX = marginLeft + (imgLeft == null ? 0 : (imgLeft.getMeasuredWidth() + marginGap));
        } else if (lineType == LineType.TYPE_DOUBLE) {
            startX = 1;
            canvas.drawLine(startX, 1, getWidth() - 1, 1, linePaint);
        } else if (lineType == LineType.TYPE_DOUBLE_LEAVE_DOWN) {
            startX = 1;
            canvas.drawLine(startX, 1, getWidth() - 1, 1, linePaint);
            startX = marginLeft + (imgLeft == null ? 0 : (imgLeft.getMeasuredWidth() + marginGap));
        }
        canvas.drawLine(startX, getHeight() - 1, getWidth() - 1, getHeight() - 1, linePaint);
    }


    /**
     * set text
     */
    public void setText(String msg) {
        if (this.content == null) return;
        if (StringUtils.isEmpty(msg)) msg = "";
        this.content.setText(msg);
    }

    public void setSubText(String msg) {
        if (this.subContent == null) return;
        if (StringUtils.isEmpty(msg)) msg = "";
        this.subContent.setText(msg);
    }

    /**
     * line type
     */
    public static class LineType {
        //line type
        public static final int TYPE_FILL = 0;
        public static final int TYPE_LEAVE_LEFT = 1;
        public static final int TYPE_DOUBLE = 2;
        public static final int TYPE_DOUBLE_LEAVE_DOWN = 3;
        public static final int TYPE_NOT = 4;
    }

    /**
     * use as a button ,common control or update control
     */
    public static class Mode {
        //line type
        public static final int COMMON = 0;
        public static final int UPDATE = 1;
        public static final int BUTTON = 2;
    }

}
